package cn.zeng;

import cn.zeng.dto.*;
import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static freemarker.template.Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS;

/**
 * @author zyb
 */
public class JavaToPdf {
    private static final String DEST = "封面.pdf";
    private static final String HTML = "cover.html";
    private static final String PATH = Objects.requireNonNull(JavaToPdf.class.getClassLoader().getResource("html")).getPath();
    private static final String LOGO_PATH = "file://" + (Objects.requireNonNull(JavaToPdf.class.getClassLoader().getResource("html")).getPath());


    private static Configuration freemarkerCfg;

    static {
        freemarkerCfg = new Configuration(DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        try {
            freemarkerCfg.setDirectoryForTemplateLoading(Paths.get(PATH).toFile());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws Exception {
        Map<String, Object> data = new HashMap<>();
        data.put("basePath", LOGO_PATH);
        data.putAll(new AssessUser().toMap());
        data.putAll(new BloodFat().toMap());
        data.putAll(new Cardiovascular().toMap());
        data.putAll(new Dangerous().toMap());
        data.putAll(new Diabetes().toMap());
        data.putAll(new DietaryAnalysis().toMap());
        data.putAll(new HealthInformationSummary().toMap());
        data.putAll(new HighBloodPressure().toMap());
        data.putAll(new LungCancer().toMap());
        data.putAll(new Metabolic().toMap());
        data.putAll(new PhysicalActivityAnalysis().toMap());
        data.putAll(new WayOfLife().toMap());
        String content = JavaToPdf.freeMarkerRender(data, "cover.html");
        content = content.concat(JavaToPdf.freeMarkerRender(data, "catalog.html"));
        content = content.concat(JavaToPdf.freeMarkerRender(data, "firstpage.html"));
        content = content.concat(JavaToPdf.freeMarkerRender(data, "second.html"));
        content = content.concat(JavaToPdf.freeMarkerRender(data, "second2.html"));
        content = content.concat(JavaToPdf.freeMarkerRender(data, "third.html"));
        content = content.concat(JavaToPdf.freeMarkerRender(data, "fourth.html"));
        content = content.concat(JavaToPdf.freeMarkerRender(data, "fifth.html"));
        content = content.concat(JavaToPdf.freeMarkerRender(data, "sixth.html"));
        content = content.concat(JavaToPdf.freeMarkerRender(data, "seventh.html"));
        content = content.concat(JavaToPdf.freeMarkerRender(data, "eighth.html"));
        content = content.concat(JavaToPdf.freeMarkerRender(data, "ninth.html"));
        content = content.concat(JavaToPdf.freeMarkerRender(data, "tenth1.html"));
        content = content.concat(JavaToPdf.freeMarkerRender(data, "tenth2.html"));
        content = content.concat(JavaToPdf.freeMarkerRender(data, "tenth3.html"));
        content = content.concat(JavaToPdf.freeMarkerRender(data, "eleventh1.html"));
        content = content.concat(JavaToPdf.freeMarkerRender(data, "eleventh2.html"));
        content = content.concat(JavaToPdf.freeMarkerRender(data, "eleventh3.html"));
        JavaToPdf.createPdf1(content, DEST);
    }

    //private static void createPdf(String content, String dest) throws Exception {
    //    ITextRenderer renderer = new ITextRenderer();
    //    ITextFontResolver fontResolver = renderer.getFontResolver();
    //    fontResolver.addFont(FONT, "songti", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED, null);
    //    renderer.setDocumentFromString(content);
    //
    //    renderer.getSharedContext().setBaseURL(LOGO_PATH);
    //    renderer.layout();
    //    renderer.createPDF(new FileOutputStream(dest));
    //}

    private static void createPdf1(String content, String dest) throws Exception {

        // step 1
        Document document = new Document(PageSize.A4, 30, 30, 30, 30);
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        // step 3
        document.open();
        // step 4
        XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
        fontImp.register("PingFang Regular.ttf");
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new ByteArrayInputStream(content.getBytes()), null, Charset.forName("UTF-8"), fontImp);
        // step 5
        document.close();
    }

    private static String freeMarkerRender(Map<String, Object> data, String html) {
        try (StringWriter stringWriter = new StringWriter()) {
            Template template = freemarkerCfg.getTemplate(html, null, null, "UTF-8", true, false);
            template.process(data, stringWriter);
            stringWriter.flush();
            return stringWriter.toString();
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        }
        return null;
    }

    //public static void main(String[] args) throws Exception {
    //    String path = JavaToPdf.class.getClassLoader().getResource(HTML).getPath();
    //
    //    // step 1
    //    Document document = new Document();
    //    // step 2
    //    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
    //    // step 3
    //    document.open();
    //    // step 4
    //    XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
    //    fontImp.register(FONT, "songti");
    //    XMLWorkerHelper.getInstance().parseXHtml(writer, document,
    //            new FileInputStream(path), null, Charset.forName("UTF-8"), fontImp);
    //    // step 5
    //    document.close();
    //
    //}
}
